package com.me.jingdongshop.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Message;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LayoutAnimationController;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckedTextView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.me.common.base.BaseActivity;
import com.me.common.base.Global;
import com.me.jingdongshop.R;
import com.me.jingdongshop.model.bean.ProductList;
import com.me.jingdongshop.model.protocol.IHttpService;
import com.me.jingdongshop.presenter.ProductlistActivityPresenter;
import com.me.jingdongshop.presenter.TestFragmentPre;
import com.me.jingdongshop.ui.adapter.ProductRVAdapter;

import java.util.List;

/**
 * Created by Tm on 2017/5/8.
 */

public class ProductlistActivity extends BaseActivity {

    private RelativeLayout rtInput;
    private TextView tvCompre;
    private ImageView imgCompre;
    private TextView tvSales;
    private TextView tvPrice;
    private ImageView img1Price;
    private ImageView img2Price;
    private TextView tvFiltrate;
    private LinearLayout llLv;
    private ListView lvOrder;
    private RecyclerView RvGoods;
    private LinearLayoutManager manager;
    private ImageView backBtn;
    private ImageView imgChangeview;
    private String mKeyword;
    private ProductRVAdapter mRvAdapter;

    //监听recyclerView 的布局状态
    int changeView = 0;
    //recyclerView 各个状态的布局资源id
    int[] resIds = {R.layout.productrv_item_ll,R.layout.productrv_item2_grid};

    //记录综合按钮的图片修改状态,共有三种状态
    int comIcom = 1;

    //记录价格按钮的图片修改状态,共有三种状态
    int priceIcom = 1;

    //记录四个按钮的字体颜色
    int btnColor = 1;



    String [] stateDatas = {"综合","上架时间","评论从高到低"};





    private GridLayoutManager gridLayoutManager;
    private ProductRVAdapter rvAdapter;
    private List<ProductList.ProductListBean> productList1;
    private LinearLayout llCompre;
    private ProductlistActivityPresenter presenter;
    private DrawerLayout drawerLayout;

    //列表动画gird
    public void amiantion(){

        TranslateAnimation  animation = new TranslateAnimation(-300f, 0f, 100f, 0f);
        animation.setDuration(150);
        //1f为延时
        LayoutAnimationController controller = new LayoutAnimationController(animation, 0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        RvGoods.setLayoutAnimation(controller);
       // rvAdapter.notifyDataSetChanged();


       /* animation = new Rotate3dAnimation(0, 360, 200, 200, 0, true);
        animation.setDuration(1000);
        controller = new LayoutAnimationController(animation, 0.1f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        mListView.setLayoutAnimation(controller);
        adapter.notifyDataSetInvalidated();
        break;*/
    }
    //列表动画ll

    protected void amiantion2(){
        LayoutAnimationController controller;
        ScaleAnimation anim = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//从0.5倍放大到1倍
        anim.setDuration(500);
        controller=new LayoutAnimationController(anim,0.1f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        RvGoods.setLayoutAnimation(controller);
    }



    @Override
    public int getLayoutRes() {
        return R.layout.activity_productlist;
    }

    @Override
    public void initView() {
        backBtn = (ImageView) findViewById(R.id.btn_back);
        imgChangeview= (ImageView) findViewById(R.id.img_changeview);


        rtInput = (RelativeLayout) findViewById(R.id.rt_input);

        llCompre = (LinearLayout) findViewById(R.id.com_ll);
        tvCompre = (TextView) findViewById(R.id.tv_compre);
        imgCompre = (ImageView) findViewById(R.id.img_compre);

        tvSales = (TextView) findViewById(R.id.tv_sales);

        tvPrice = (TextView) findViewById(R.id.tv_price);
        img1Price = (ImageView) findViewById(R.id.img1_price);
        img2Price = (ImageView) findViewById(R.id.img2_price);

        tvFiltrate = (TextView) findViewById(R.id.tv_filtrate);

        llLv = (LinearLayout) findViewById(R.id.ll_lv);
        lvOrder = (ListView) findViewById(R.id.lv_order);
        RvGoods = (RecyclerView) findViewById(R.id.Rv_goods);
        drawerLayout = (DrawerLayout) findViewById(R.id.filtrate_layout);


        //初始化orderLv
        ArrayAdapter arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,stateDatas);
        lvOrder.setAdapter(arrayAdapter);
        lvOrder.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        lvOrder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CheckedTextView box  = (CheckedTextView) view;

                if (position == 0){
                    //默认选择
                    presenter.getpriceDown();
                }else if (position == 1){
                    //上架时间
                    presenter.getshelvesDown();

                }else if (position==2){
                    //评论高低
                    presenter.getpriceUp();
                }

                for (int i = 0; i < parent.getChildCount() ; i++) {
                    if (i == position){
                        box.setTextColor(Color.RED);
                        tvCompre.setText(stateDatas[position]);

                    }else {
                        CheckedTextView box1  = (CheckedTextView) parent.getChildAt(i);
                        box1.setTextColor(Color.BLACK);
                    }

                }


            }
        });



        manager = new LinearLayoutManager(this);
        gridLayoutManager = new GridLayoutManager(this,2);
        //返回按钮
        backBtn.setOnClickListener(this);

        //列表布局变化按钮
        imgChangeview.setOnClickListener(this);

        //综合按钮
        llCompre.setOnClickListener(this);

        //价格按钮
        tvPrice.setOnClickListener(this);

        //销量按钮
        tvSales.setOnClickListener(this);

        //筛选
        tvFiltrate.setOnClickListener(this);
        llLv.setOnClickListener(this);

        rtInput.setOnClickListener(this);

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

        presenter = new ProductlistActivityPresenter(this);
        mKeyword = getIntent().getStringExtra("keyword");
//        mKeyword ="外套";
        if (mKeyword != null) {
            TestFragmentPre testFragmentPre  = new TestFragmentPre(this);
            testFragmentPre.getSearchData(1,10,"saleDown",mKeyword);
        }else{
            //第一次获取的数据
            presenter.getFirstData();
            rvAdapter = new ProductRVAdapter(this, null,resIds[changeView]);
            RvGoods.setLayoutManager(manager);
            RvGoods.setAdapter(rvAdapter);

            amiantion();
        }

    }

    @Override
    public void onClick(View v, int id) {
        switch (id){
            case R.id.img_changeview:        //列表布局变化按钮
                if (changeView==0){

                    RvGoods.setLayoutManager(manager);

                    rvAdapter = new ProductRVAdapter(this, productList1,resIds[changeView]);
                    RvGoods.setAdapter(rvAdapter);
                    amiantion();
                    changeView=1;
                    imgChangeview.setImageResource(R.drawable.user_photo);


                }else {
                    RvGoods.setLayoutManager(gridLayoutManager);
                    rvAdapter = new ProductRVAdapter(this, productList1,resIds[changeView]);
                    RvGoods.setAdapter(rvAdapter);
                    amiantion2();
                    changeView=0;
                    imgChangeview.setImageResource(R.drawable.icon_share);
                }
                llLv.setVisibility(View.GONE);
                comIcom =1;
                priceIcom =1;
                break;
            case R.id.com_ll:            //综合按钮

                if (comIcom ==1){
                    comIcom=2;

                }else if (comIcom==2){
                    comIcom=3;
                }else if (comIcom==3){
                    comIcom=2;
                }
                btnColor=1;
                priceIcom =1;

                break;
             case R.id.tv_price:            //价格按钮
                 tvPrice.setTextColor(Color.RED);

                 if (priceIcom ==1){
                     priceIcom=2;

                 }else if (priceIcom==2){
                     priceIcom=3;
                 }else if (priceIcom==3){
                     priceIcom=2;
                 }

                 btnColor=2;

                 comIcom =1;

            break;
            case R.id.tv_sales:             //销量按钮
                tvSales.setTextColor(Color.RED);
                presenter.getFirstData();

                btnColor=3;
                comIcom =1;
                priceIcom =1;
                break;
            case R.id.tv_filtrate:          //筛选按钮
                llLv.setVisibility(View.GONE);
                drawerLayout.openDrawer(Gravity.RIGHT);

                break;
            case R.id.ll_lv:
                llLv.setVisibility(View.VISIBLE);
                comIcom=3;
                break;
            case R.id.rt_input:
                Intent intent = new Intent(this,HomeSearchPagerActivity.class);
                startActivity(intent);
                break;


        }
        //实现综合按钮的图片状态转换
        if (comIcom==1){
            imgCompre.setImageResource(R.drawable.arrow_dowm1);
            llLv.setVisibility(View.GONE);

        }else if (comIcom==2){
            imgCompre.setImageResource(R.drawable.arrow_up2);
            llLv.setVisibility(View.VISIBLE);

        }else {
            imgCompre.setImageResource(R.drawable.arrow_dowm2);
            llLv.setVisibility(View.GONE);

        }


        //实现价钱按钮的图片状态转换
        if (priceIcom==1){
            img1Price.setImageResource(R.drawable.arrow_up1);
            img2Price.setImageResource(R.drawable.arrow_dowm1);

        }else if (priceIcom==2){
            img1Price.setImageResource(R.drawable.arrow_up2);
            img2Price.setImageResource(R.drawable.arrow_dowm1);

            presenter.getpriceUp();

        }else {
            img1Price.setImageResource(R.drawable.arrow_up1);
            img2Price.setImageResource(R.drawable.arrow_dowm2);

            presenter.getpriceDown();

        }
        //实现四个按钮的字体颜色
        if (btnColor==1){

            tvCompre.setTextColor(Color.RED);
            tvPrice.setTextColor(Color.GRAY);
            tvSales.setTextColor(Color.GRAY);

        }else if (btnColor==2){

            tvCompre.setTextColor(Color.GRAY);
            tvPrice.setTextColor(Color.RED);
            tvSales.setTextColor(Color.GRAY);

        }else if (btnColor==3){

            tvCompre.setTextColor(Color.GRAY);
            tvPrice.setTextColor(Color.GRAY);
            tvSales.setTextColor(Color.RED);

        }


    }



    @Override
    public void onHttpSuccess(int reqType, Message msg) {
        //不要删
        if (reqType == IHttpService.HTTP_SEARCH){
            ProductList bean = (ProductList) msg.obj;
            List<ProductList.ProductListBean> productList2 = bean.getProductList();
            mRvAdapter = new ProductRVAdapter(this, productList2,resIds[1]);
            RvGoods.setLayoutManager(manager);
            RvGoods.setAdapter(mRvAdapter);
            return;
        }

        ProductList productList = (ProductList) msg.obj;
        productList1 = productList.getProductList();


        if (reqType == IHttpService.HTTP_PRODUCTLIST){

            rvAdapter.setDatas(productList1);



        }else if (reqType ==IHttpService.HTTP_PRODUCTLIST_priceUp){
            rvAdapter.setDatas(productList1);
           // amiantion();

        }else if (reqType ==IHttpService.HTTP_PRODUCTLIST_priceDown){
            rvAdapter.setDatas(productList1);
            //amiantion();

        }else if (reqType ==103){         //IHttpService.HTTP_PRODUCTLIST_commentDown
            rvAdapter.setDatas(productList1);
           // amiantion();

        }else if (reqType ==IHttpService.HTTP_PRODUCTLIST_shelvesDown){
            rvAdapter.setDatas(productList1);
           // amiantion();


        }


            amiantion();



    }

    @Override
    public void onHttpError(int reqType, String error) {
        if (reqType ==IHttpService.HTTP_SEARCH ){
            Toast.makeText(Global.mContext,error,Toast.LENGTH_SHORT).show();
        }
    }
}
